package yl.hs.bmipfull.service.salemanage.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.salemanage.IProductPricesDao;
import yl.hs.bmipfull.pojo.salemanage.ProductPricesEntity;
import yl.hs.bmipfull.service.salemanage.IProductPricesService;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.VoUtil;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;
import yl.hs.bmipfull.viewmodels.salemanmage.TimeGroupVO;

import java.util.List;
import java.util.Map;

/**
 * 作用:  产品信息价服务层接口实现<br/>
 * 创建时间: 2021年06月08日 09时27分20秒 <br/>
 *
 * @author 张凯
 */
@Service
public class ProductPricesServiceImpl extends SaleBaseService implements IProductPricesService {

    @Autowired
    IProductPricesDao dao;

    @Override
    public List<ProductPricesEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(ProductPricesEntity.class);
        return dao.queryList(pageQuery.getQuery());
    }

    @Override
    public PageResult<Map<String,Object>> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(ProductPricesEntity.class);
        PageResult<Map<String,Object>> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), ProductPricesEntity.class);
        pageQuery.getQuery().put("unitSNArray", getCompanySNList());
        result.setData(dao.queryProductPriceView(pageQuery.getQuery(),filters));
        return result;
    }


    @Override
    public ProductPricesEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional
    public Integer saveProductPrices(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        ProductPricesEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new ProductPricesEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setOrganizationCode(getDepartSN());
            entity.setSn(queryNewSN(ProductPricesEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入产品信息价信息", Tl.toJson(entity), ProductPricesEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新产品信息价信息", Tl.toJson(entity), ProductPricesEntity.class);
            }
        }

        return result;
    }

    @Override
    public List<Map<String,Object>> findProductPriceBySn(String productSN) {
        return dao.findProductPriceList(productSN);
    }

    @Override
    public List<TimeGroupVO> getSetTimeGroup() {
        return VoUtil.groupTime(dao.getSetTime());
    }

    @Override
    public Integer deleteBySN(String sn) {
        return dao.deleteBySN(sn);
    }
}